﻿using eProcurement.BLL;
using eProcurement.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace eProcurement.members
{
    public partial class PlaceBid : System.Web.UI.Page
    {
        Manager manager = new Manager();

        protected void Page_Load( object sender, EventArgs e )
        {
            if ( !IsPostBack )
            {
                Project project = manager.GetProject( Convert.ToInt32( Request[ "pid" ] ) );
                if ( project != null )
                {
                    Hydrate( project );
                }
            }
        }

        private void Hydrate( Project project )
        {
            lblCompanyName.InnerText = project.Purchaser.CompanyName;
            lblAddress1.InnerText = project.Purchaser.Address1;
            lblAddress2.InnerText = project.Purchaser.Address2;
            lblCity.InnerText = project.Purchaser.City;
            lblWebsite.InnerHtml = "<a href='" + project.Purchaser.Website + "'>" + project.Purchaser.Website + "</a>";
            lblDescription.InnerText = project.Purchaser.Description;

            lblProjectName.InnerText = project.Title;
            lblProjectDescription.InnerText = project.Description;
            lblIndustryCategory.InnerText = project.IndustryCategory;
            lblClosureDate.InnerText = project.CloseDate.ToString( "ddd d MMM yyyy" );
            lblPostDate.InnerText = project.DatePosted.ToString( "ddd d MMM yyyy" );
        }

        protected void btnSubmit_Click( object sender, EventArgs e )
        {
            Bid bid = new Bid();
            bid.ProjectId = Convert.ToInt32( Request[ "pid" ] );
            bid.SupplierId = manager.GetAppUser( Context.User.Identity.Name ).Id;
            bid.Documents = FileUpload1.FileBytes;
            bid.Filename = FileUpload1.FileName;
            bid.IsWinner = false;

            if ( manager.PlaceBid( bid ) )
            {
                tipSaved.Visible = true;
            }
        }
    }
}